#include "pch.h"
#include "DrawCube.h"
#include"DrawLine.h"
#define _USE_MATH_DEFINES
#include<math.h>
#include<algorithm>
void DrawCube::draw(CDC* pDC)
{
	Point seenPoints[8];
	for (int i = 0; i < 8; i++)
	{
		seenPoints[i].x = pointsTable[i].x + 
			(pointsTable[i].x - watchPoint.x) * d / (pointsTable[i].z + d);
		seenPoints[i].y = pointsTable[i].y + 
			(pointsTable[i].y - watchPoint.y) * d / (pointsTable[i].z + d);
		seenPoints[i].z = d;
	}
	for (int i = 0; i < 8; i++)
	{
		seenPoints[i].x = seenPoints[i].x + 700;
		seenPoints[i].y = seenPoints[i].y + 400;
	}
	DrawLine line;
	for (int i = 0; i < 12; i++)
	{
		line.draw(pDC,
			seenPoints[edgesTable[i].p1].x,
			seenPoints[edgesTable[i].p1].y,
			seenPoints[edgesTable[i].p2].x,
			seenPoints[edgesTable[i].p2].y,
			RGB(0,0,0));
	}
}

void DrawCube::rotate(CDC* pDC, int center, int angle)
{
	double anglef = angle * M_PI / 180;
	pDC->SetROP2(R2_WHITE);
	this->draw(pDC);
	double x = 0, y = 0, z = 0;
	switch (center)
	{
	case X:
		for (int i = 0; i < 8; i++)
		{
			x = this->pointsTable[i].x;
			y = this->pointsTable[i].y;
			z = this->pointsTable[i].z;

			this->pointsTable[i].x= x;
			this->pointsTable[i].y = (double)y * cos(anglef)- (double)z * sin(anglef);
			this->pointsTable[i].z = (double)y * sin(anglef) + (double)z * cos(anglef);

		}
		break;
	case Y:
		for (int i = 0; i < 8; i++)
		{
			x = this->pointsTable[i].x;
			y = this->pointsTable[i].y;
			z = this->pointsTable[i].z;

			this->pointsTable[i].x = (x * cos(anglef) + z * sin(anglef));
			this->pointsTable[i].y = y;
			this->pointsTable[i].z = (-1* x * sin(anglef) + z * cos(anglef));
		}
		break;
	case Z:
		for (int i = 0; i < 8; i++)
		{
			x = this->pointsTable[i].x;
			y = this->pointsTable[i].y;
			z = this->pointsTable[i].z;

			this->pointsTable[i].x = (x * cos(anglef) - y * sin(anglef));
			this->pointsTable[i].y = (x * sin(anglef) + y * cos(anglef));
			this->pointsTable[i].z = z;
		}
		break;
	default:
		break;
	}
	pDC->SetROP2(R2_BLACK);
	this->draw(pDC);
}

void DrawCube::move(CDC* pDC, int center, int length)
{
	pDC->SetROP2(R2_WHITE);
	this->draw(pDC);
	switch (center)
	{
	case X:
		for (int i = 0; i < 8; i++)
		{
			this->pointsTable[i].x += length;
		}
		break;
	case Y:
		for (int i = 0; i < 8; i++)
		{
			this->pointsTable[i].y += length;
		}
		break;
	case Z:
		for (int i = 0; i < 8; i++)
		{
			this->pointsTable[i].z += length;
		}
		break;
	default:
		break;
	}
	pDC->SetROP2(R2_BLACK);
	this->draw(pDC);
}

void DrawCube::setMode(MODE m)
{
	MODE_NOW = m;
}


int DrawCube::draw(CDC* pDC, int startx, int starty, int endx, int endy, COLORREF color)
{
	return 0;
}

DrawCube::DrawCube()
{
	this->pointsTable[0] = { 200,0,0 };
	this->pointsTable[1] = { 200,200,0 };
	this->pointsTable[2] = { 0,200,0 };
	this->pointsTable[3] = { 0,0,0 };
	this->pointsTable[4] = { 200,0,200 };
	this->pointsTable[5] = { 200,200,200 };
	this->pointsTable[6] = { 0,200,200 };
	this->pointsTable[7] = { 0,0,200 };

	edgesTable[0] = { 0,1 };
	edgesTable[1] = { 1,2 };
	edgesTable[2] = { 2,3 };
	edgesTable[3] = { 3,0 };
	edgesTable[4] = { 4,5 };
	edgesTable[5] = { 5,6 };
	edgesTable[6] = { 6,7 };
	edgesTable[7] = { 7,4 };
	edgesTable[8] = { 0,4 };
	edgesTable[9] = { 1,5 };
	edgesTable[10] = { 2,6 };
	edgesTable[11] = { 3,7 };

	surfaceTable[0] = { 0,9,4,8 };
	surfaceTable[1] = { 1,10,5,9 };
	surfaceTable[2] = { 2,11,6,10 };
	surfaceTable[3] = { 3,8,7,11 };
	surfaceTable[4] = { 4,5,6,7 };
	surfaceTable[5] = { 2,1,0,3 };

	MODE_NOW = MODE::MOVE;
}
